package guide;

import org.junit.Test;

/**
 * @author fance
 * @date 18-4-27 上午6:41
 */
public class StringSum {

    /**
     * 是否互为变形词  1.sort before equals 2.保存每个字符出现的次数
     * @param s1
     * @param s2
     * @return
     */
    public boolean isDeformation(String s1, String s2) {
        if (s1 == null || s2 == null || s1.length() != s2.length()) {
            return false;
        }
        int[] map = new int[256];
        for (int i = 0; i < s1.length(); i++) {
            map[s1.charAt(i)]++;
        }
        for (int i = 0; i < s2.length(); i++) {
            if (map[s2.charAt(i)]-- == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 字符串中数字字符的求和
     * @param s
     * @return
     */
    public int numSum(String s) {
        if (s == null) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int res = 0;
        int num = 0;
        boolean posi = true;
        int cur = 0;
        for (int i = 0; i < chars.length; i++) {
            cur = chars[i] - '0';
            if (cur < 0 || cur > 9) {
                res += num;
                num = 0;
                if (chars[i] == '-') {
                    if (i - 1 >= 0 && chars[i - 1] == '-') {
                        posi = !posi;
                    } else {
                        posi = false;
                    }
                } else {
                    posi = true;
                }
            } else {
                num = num * 10 + (posi ? cur : -cur);
            }
        }
        res += num;
        return res;
    }

    /**
     * 是否为旋转词  最优解: kmp
     * @param a
     * @param b
     * @return
     */
    public boolean isRotation(String a, String b) {
        return (a + a).contains(b);
    }

    /**
     * 去掉字符串中连续出现k个0的子串
     * @param s
     * @param k
     * @return
     */
    public String removeKZeros(String s, int k) {
        if (s == null || k < 1) {
            return s;
        }
        char[] chars = s.toCharArray();
        int cnt = 0;
        int start = -1;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '0') {
                cnt++;
                start = start == -1 ? i : start;
            } else {
                if (cnt == k) {
                    while (cnt-- != 0) {
                        chars[start++] = 0;
                    }
                }
                cnt = 0;
                start = -1;
            }
        }
        if (cnt == k) {
            while (cnt-- != 0) {
                chars[start++] = 0;
            }
        }
        return String.valueOf(chars);
    }

    /**
     * 字符串匹配
     * @param str
     * @param exp
     * @return
     */
    public boolean isMatch(String str, String exp) {
        if (str == null || exp == null) {
            return false;
        }
        char[] s = str.toCharArray();
        char[] e = exp.toCharArray();
        return isValid(s,e) ? processForIsMatch(s,e,0,0) : false;

    }
    private boolean processForIsMatch(char[] s, char[] e, int si, int ei) {
        if (ei == e.length) {
            return si == s.length;
        }
        if (ei + 1 == e.length || e[ei + 1] != '*') {
            return si != s.length && (e[ei] == s[si] || e[ei] == '.') && processForIsMatch(s, e,si + 1 , ei + 1);
        }
        while (si != s.length && (e[ei] == s[si] || e[ei] == '.')) {
            if (processForIsMatch(s,e,si,ei + 2)) {
                return true;
            }
            si++;
        }
        return processForIsMatch(s,e,si,ei + 2);
    }
    private boolean isValid(char[] s, char[] e) {
        for (int i = 0; i < s.length; i++) {
            if (s[i] == '*' || s[i] == '.') {
                return false;
            }
        }
        for (int i = 0; i < e.length; i++) {
            if (e[i] == '*' && (i == 0 || e[i - 1] == '*')) {
                return false;
            }
        }
        return true;
    }


    /**
     * 所有字符是否只出现一次 1.排序 比较 2.哈系表
     * @param s
     * @return
     */
    public boolean isUnique(String s) {
        if (s == null) {
            return true;
        }
        boolean[] map = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            if (map[s.charAt(i)]) {
                return false;
            }
            map[s.charAt(i)] = true;
        }
        return true;
    }

    @Test
    public void test() {
        // System.out.println(isRotation("123","312"));
        System.out.println(removeKZeros("a00b", 2));
    }
}
